#ifndef BOARD_H
#define BOARD_H
#include "chessDef.h"
#include "pos.h"
#include "bmp.h"
#include "lcd.h"
class Board
{
    //复制初始棋盘并且是可修改的
    ChessDef::pieceID currentBoard[9][10];
public:
    Board()
    {
        for(int i = 0; i < 9; i++)
        {
            for(int j = 0; j < 10; j++)
            {
                currentBoard[i][j] = ChessDef::initBoard[i][j];
            }
        }
    }
    // 获取指定位置的棋子
    ChessDef::pieceID getPieceAt(Pos p) const 
    {
        if (p.row < 0 || p.row >= 9 || p.col < 0 || p.col >= 10) 
        {
            return ChessDef::pieceID::kong;
        }
        return currentBoard[p.row][p.col];
    }
    //设置指定位置棋子
    void setPieceAt(const Pos& pos, int pieceInt) 
    {
        // 校验坐标合法性（避免越界）
        if (pos.row >= 0 && pos.row < 9 && pos.col >= 0 && pos.col < 10) 
        {
            // 2. 将int转换为枚举类型
            ChessDef::pieceID piece = static_cast<ChessDef::pieceID>(pieceInt);
            currentBoard[pos.row][pos.col] = piece;
        }
    }
    //判断基础规则，随后更新棋盘（在这之前会先进行规则校验）
    bool movePiece(Pos from, Pos to)
    {
        //如果起点为空，移动失败
        ChessDef::pieceID srcPiece = getPieceAt(from); 
        if(srcPiece==ChessDef::kong)
        {
            return false;
        }
        ChessDef::pieceID destPiece = getPieceAt(to);
        //判断终点是不是我方棋子
        if(destPiece!=ChessDef::kong)
        {
            bool srcIsRed = ChessDef::isRed(srcPiece);
            bool destIsRed = ChessDef::isRed(destPiece);
            if(srcIsRed == destIsRed)
            {
                return false;
            }
        }
        //更新棋盘
        currentBoard[to.row][to.col] = srcPiece;
        currentBoard[from.row][from.col] = ChessDef::kong;
        return true;
    }
    //重置棋盘的功能
    void reset()
    {
        for (int row = 0; row < 9; row++) 
        {
            for (int col = 0; col < 10; col++) 
            {
                currentBoard[row][col] = ChessDef::initBoard[row][col];
            }
        }
    }
};



#endif